/* Teach Tool  - Structural Dynamics
 * Copyright (C) 2009: Fabian Gerold, www.fabiangerold.de
 *
 * This file may be licensed under the terms of of the
 * GNU General Public License Version 3 (the "GPL").
 * 
 * Software distributed under the License is distributed 
 * on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
 * express or implied. See the GPL for the specific language 
 * governing rights and limitations.
 *
 * You should have received a copy of the GPL along with this 
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
 * or write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Diagram.h"
#include "DiagramScroll.h"

DiagramScroll::DiagramScroll( System* sys, QWidget *parent ) : QScrollArea( parent )
{
	m_diag = new Diagram( sys );
	m_xpos = 0;
	connect( &m_timer, SIGNAL(timeout()), this, SLOT( slotMove() ) );
	m_time.start();

	setWidget( m_diag );
	setWidgetResizable( false );
	setMinimumHeight( 200 );
	setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
	setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
	setFrameStyle( QFrame::Plain );
	setFrameShadow( QFrame::Plain );
}

QSize DiagramScroll::sizeHint() const
{
	return QSize( 900, 200 );
}

QSize DiagramScroll::minimumSizeHint() const
{
	return QSize( 500, 200 );
}

void DiagramScroll::wheelEvent( QWheelEvent* event )
{
	int t = event->delta()*0.5;
	
	if( m_xpos + t > 0 )
		t = -m_xpos;


	if( m_xpos + t - width() < -3060 )
		t = -m_xpos - 3060 + width();

	viewport()->scroll( t, 0 );
	m_xpos += t;

}

void DiagramScroll::mousePressEvent( QMouseEvent* event )
{
	setCursor( QCursor( Qt::ClosedHandCursor ) );
	//QApplication::setOverrideCursor( QCursor( Qt::ClosedHandCursor ) );
	m_lastpos = event->x();
	m_timer.stop();
	m_time.restart();
	m_t_last = 0.0;

}
void DiagramScroll::mouseReleaseEvent( QMouseEvent* )
{
	setCursor( QCursor( Qt::OpenHandCursor ) );
	if( m_v0 > 20.0 )
		m_v0 = 20.0;
	if( m_v0 < -20.0 )
		m_v0 = -20.0;
		
	m_timer.start(10);
	m_time.restart();
	m_t_last = 0.0;

}
void DiagramScroll::mouseMoveEvent ( QMouseEvent* event )
{
	//int diff = event->x() - m_lastpos;
	//m_lastpos = event->x();

	//if( m_xpos + diff > 0 )
	//	diff = -m_xpos;


	//if( m_xpos + diff - width() < -2060 )
	//	diff = -m_xpos - 2060 + width();

	//viewport()->scroll( diff, 0 );
	//m_xpos += diff;


		int diff = event->x() - m_lastpos;
		m_v0 = diff;
		
		m_lastpos = event->x();
		moveDiag( diff );
		m_time.restart();

}

void DiagramScroll::moveDiag( int diff )
{
	if( m_xpos + diff > 0 )
		diff = -m_xpos;

	if( m_xpos + diff - width() < -3060 )
		diff = -m_xpos - 3060 + width();

	viewport()->scroll( diff, 0 );
	m_xpos += diff;
}

void DiagramScroll::srollToZero()
{
	viewport()->scroll( -m_xpos, 0 );
	m_xpos = 0;
}

void DiagramScroll::resizeEvent( QResizeEvent* event )
{
	m_diag->resizeEvent( event );
}

void DiagramScroll::slotMove()
{
	double t = m_time.elapsed();
	double velocity;
	
	if( m_v0 > 0.0 )
	{
		velocity = m_v0 - 0.04*t;
		if( velocity < 0.0 )
		{
			velocity = 0.0;
			m_timer.stop();
		}
	}
	else
	{
		velocity = m_v0 + 0.04*t;
		if( velocity > 0.0 )
		{
			velocity = 0.0;
			m_timer.stop();
		}
	}
	
	double dt = t - m_t_last;
	m_t_last = t;
	moveDiag( (int)(velocity*dt*0.08) );
}
